package design_pattern.study.prototype.deep_copy;

import java.io.*;

public class test {
    public static void main(String[] args) {
        Citation citation = new Citation();
        Student student = new Student("zs", "学生");
        citation.setStu(student);

        ByteArrayOutputStream bos = null;
        ObjectOutputStream objectOutputStream = null;
        //使用对象流进行深拷贝
        //写入
        try {
            bos = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(bos);
            objectOutputStream.writeObject(citation);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (objectOutputStream != null) {
                    objectOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //读取
        ObjectInputStream objectInputStream = null;
        try {
            if (bos.size() == 0) {
                throw new RuntimeException("读取失败，null");
            }
            objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
            Citation o = (Citation) objectInputStream.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (objectInputStream != null) {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
